
/*******************************************************************************
 * The code below this comment is integral part of the project "Jelliphy".
 * 
 * "Jelliphy" is a project created by: 
 * Dispage - Patrizio Gelosi
 * Via A. De Gasperi 91 
 * P. Potenza Picena (MC) - Italy
 * 
 * (Hereby referred to as "DISPAGE")
 * 
 * Copyright (c) 2011 DISPAGE.
 * 
 * @Released on: 02-06-2011
 * @Version: 1.0.00
 * @Build: 1109
 * @Site: http://www.dispage.com
 * @Contacts: http://www.dispage.com/contacts
 * 
 * The contents of this file are released under the GNU General Public License
 * version 3 as published by the Free Software Foundation that can be found at
 * the address "http://opensource.org/licenses/gpl-3.0.html". If the license
 * cannot be found, write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * You may not use the present file except in compliance with the License.
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * Dispage" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by Dispage".
 * 
 ******************************************************************************/

var MGUtils = {

	charList : '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
	addTimeChar : 5,
	addRNDChar : 5,


	cutText : function (text, len, reverse) {

		if (text && text.length > len) {
			if (reverse) {
				text = "... " + text.substr(text.length - len);
			}
			else {
				text = text.substr(0, len) + " ...";
			}
		}
		return text || '';
	},

	text2string : function (text) {

		return text.replace(/[\r\n]/g, '\'+"\\n"+\'');
	},

	text2SQString : function (text) {

		if (!text) return '';

		return this.text2string(text.replace(/\'/g, "&apos;"));
	},
	
	text2QCString : function (text) {

		if (!text) return '';

		return this.text2string(text.replace(/\"/g, "&quot;"));
	},
	
	content2SQString : function (content, options) {

		options = options || {};
		if (options.quot) {
			content = this.text2QCString(content);
		}
		else {
			content = this.text2SQString(content);
		}
		content = content.replace(/^(?:\s*[\r\n]+)+/, '');
		content = content.replace(/[\r\n]+[\s\S]*$/, '');

		return content;
	},

	getWindowPosition : function (sel, add, addWidth, addHeight) {
		
		var $el = (sel && sel.jquery) ? sel : JlQ(sel),
			$window = JlQ(window),
			scrollTop = $window.scrollTop(),
			w = $el.width(),
			h = $el.height(),
			offset = $el.offset(),
			wsrc;
		
		if (addWidth) {
			if (addHeight) {
				wsrc = (typeof(addHeight) == 'number') ? addHeight : parseInt(JlQ(addHeight).css('height'));
				if (!add || !add.top) add.top = 0;
				if (offset.top - scrollTop - add.top - wsrc > 0) {
					add.top = -wsrc -add.top;
				}
				else if (offset.top - scrollTop + add.top + h + wsrc < $window.height()) {
					add.top += h;
				}
				else {
					addHeight = null;
				}
				if (addHeight) {
					add.left = w > addWidth ? ((w - addWidth) / 2) : 0;
				}
			}
			if (!addHeight) {
				wsrc = (typeof(addWidth) == 'number') ? addWidth : parseInt(JlQ(addWidth).css('width'));
				if (!add || !add.left) add.left = 0;
				if (offset.left + add.left + w + wsrc < $window.width()) {
					add.left += w;
				}
				else {
					add.left = -wsrc -add.left;
				}
			}
		}
		if (add) {
			if (add.top) offset.top += add.top;
			if (add.left) offset.left += add.left;
		}
		return {
			top: offset.top -scrollTop,
			left: offset.left -$window.scrollLeft()
		};
	},

	getUM9t : function () {

		var ut = new Date().getTime(),
			i, mod, res = '';

		for (i = 0; i < this.addTimeChar; i++) {
			mod = ut % 64;
			ut = Math.floor(ut / 64);
			res += this.charList.charAt(mod);
		}
		for (i = 0; i < this.addRNDChar; i++) {
			res += this.charList.charAt(Math.floor(Math.random()*64));
		}
		return res;
	},

	add1Digit : function (id, pos) {

		if (!pos) {
			pos = 1;
		}
		var mpos = id.length - pos,
			cid = id.split(''),
			d = cid[mpos],
			i = this.charList.indexOf(d);

		if (i >= this.charList.length -1) {
			cid[mpos] = this.charList.substr(0, 1);
			if (mpos > 0) {
				return this.add1Digit(cid.join(''), pos +1);
			}
			else {
				return this.charList.substr(0, 1) + cid.join('');
			}
		}
		else {
			cid[mpos] = this.charList.substr(i +1, 1);
		}
		return cid.join('');
	},

	decimalToHex : function (d, padding) {

		var hex = Number(d).toString(16);
		padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;

		while (hex.length < padding) {
			hex = "0" + hex;
		}
		return hex;
	},

	rgba2ahex : function (rgba) {

		var res = '';

		JlQ.each(rgba, function (i, v) {
			res += MGUtils.decimalToHex(v);
		});

		return res;
	},

	XML2Obj : function (el) {

		var elems = {};

		JlQ(el).children().each( function () {
			elems[this.nodeName] = this.textContent ? this.textContent : this.text;
		});
		return elems;
	},

	addURIParObj : function (params) {

		var res = document.location.search;

		JlQ.each(params, function (p, v) {
			
			var regexp = new RegExp("(\\?|\\&)" + p + "(?:\\=([^\\&]*)(\\&|$))?");

			if (regexp.test(res)) {
				res = (res.toString().replace(regexp, function(a, b, c, d)
				{
						return (b + p + "=" + v + d);
				}));
			}
			else {
				res += ((res.search(/\?/) == -1) ? "?" : "&" ) + p + "=" + v;
			}
		});
		return res;
	},

	removeURIParArray : function (params, cleanFragment) {

		var res = document.location.href;

		JlQ.each(params, function (i, v) {
			
			var regexp = new RegExp("([?&])" + v + "(=([^&]*))?(&|$)"),
				sd = res.match(regexp);
			
			if (sd) {
				res = (res.toString().replace(regexp, sd[4] ? sd[1] : ''));
			}
		});
		if (cleanFragment) {
			res = res.replace(/#.*$/, '');
		}
		return res;
	},

	redimImage : function (bckImage, min, max) {

		var img = new Image(),
			w, h, r;

		img.src = eval(bckImage.replace(/^\s*url\s*\(\s*[\'\"]?/i, "'").replace(/[\'\"]?\s*\)\s*$/i, "'"));
		w = img.width;
		h = img.height;
		r = w / h,
		scale = r > 0.2 && r < 5;

		if (w < min.width) {
			if (scale) h = h * (min.width / w)
			w = min.width;
		}
		if (h < min.height) {
			if (scale) w = w * (min.height / h)
			h = min.height;
		}
		if (w > max.width) {
			if (scale) h = h * (max.width / w)
			w = max.width;
		}
		if (h > max.height) {
			if (scale) w = w * (max.height / h)
			h = max.height;
		}
		return '<img src="' + img.src + '" border="0" width="' + w + '" height="' + h + '">';
	}

}
